package com.miyake.client.datamanage;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.tools.ClickableLabel;

public class ShowSubItems {
	private NodeUpdateHandler handler = null;
	private PageFactory factory = null;
	private NodeProperty node;
	private VerticalPanel panel;
	private boolean tableVisible = true;
	private boolean plotVisible = true;
	private boolean imageVisible = true;
	private boolean textVisible = true;
	private boolean dataNameVisible = false;
	private boolean recursible = true;
	
	ShowSubItems(NodeProperty node, VerticalPanel panel, NodeUpdateHandler handler) {
		this.node = node;
		this.panel = panel;
		if (handler == null) {
			factory  = new FixedPageFactory(handler);
		}
		else {
			factory = new EditablePageFactory(handler);
		}
		this.handler = handler;
	}
	
	public void showChild(final NodeProperty node, VerticalPanel panel) {
		
		if (node.getType().equals(NodeProperty.PLOT) && plotVisible) {
			panel.add(new HTML("<b>" + node.getName() + "</b>"));
			factory.createChart(node, panel);
		}
		else if (node.getType().equals(NodeProperty.IMAGE) && imageVisible) {
			panel.add(new HTML(node.getName()));
			factory.createImage(node, panel);
		}
		else if (node.getType().equals(NodeProperty.TABLE) && tableVisible) {
			panel.add(new HTML("<b>" + node.getName() + "</b>"));
			factory.createTable(node, panel);
		}
		else if (node.getType().equals(NodeProperty.TEXT) && textVisible) {
			factory.createText(node, panel);
		}
		else if (node.getType().equals(NodeProperty.ALIAS)) {
			VerticalPanel p = new VerticalPanel();
			p.setWidth("100%");
			panel.add(p);
			new ShowAlias(node.getKey(), p);
		}

		if (!recursible) {
			return;
		}
		
		for (NodeProperty n : node.getChildren()) {
			VerticalPanel p = new VerticalPanel();
			if (dataNameVisible && 
					(n.getType().equals(NodeProperty.PLOT) || n.getType().equals(NodeProperty.TABLE) || n.getType().equals(NodeProperty.IMAGE))) { 
//				node.getType().equals(NodeProperty.DATA)) {
				//p.add(new HTML("<b>" + node.getName() + "</b>"));
				p.add(new ClickableLabel(node.getName(), new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						handler.onNodeChange(node.getKey());
					}
				}));
			}
			panel.add(p);
			p.setWidth("100%");
			
			showChild(n, p);	
		}
	}

	public void update() {
		for (NodeProperty n : node.getChildren()) {
			showChild(n, panel);
		}
	}

	public boolean isTableVisible() {
		return tableVisible;
	}

	public void setTableVisible(boolean tableVisible) {
		this.tableVisible = tableVisible;
	}

	public boolean isPlotVisible() {
		return plotVisible;
	}

	public void setPlotVisible(boolean plotVisible) {
		this.plotVisible = plotVisible;
	}

	public boolean isImageVisible() {
		return imageVisible;
	}

	public void setImageVisible(boolean imageVisible) {
		this.imageVisible = imageVisible;
	}

	public boolean isTextVisible() {
		return textVisible;
	}

	public void setTextVisible(boolean textVisible) {
		this.textVisible = textVisible;
	}

	public void setDataNameVisible(boolean dataNameVisible) {
		this.dataNameVisible = dataNameVisible;
	}

	public boolean isDataNameVisible() {
		return dataNameVisible;
	}

	public void setRecursible(boolean recursible) {
		this.recursible = recursible;
	}

	public boolean isRecursible() {
		return recursible;
	}
	
}
